package com.odata.springboot.starter.odata.odata.sql.process.impl;

import com.odata.springboot.starter.odata.odata.config.OdataCachedData;
import com.odata.springboot.starter.odata.odata.sql.process.SqlProcess;
import com.odata.springboot.starter.odata.odata.sql.process.SqlProcessChain;
import com.odata.springboot.starter.odata.odata.sql.sqlBuild.OdataClassMetaData;
import com.odata.springboot.starter.odata.odata.sql.sqlBuild.OdataSqlInformation;
import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.serializer.SerializerException;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.UriResource;
import org.apache.olingo.server.api.uri.UriResourceEntitySet;
import org.apache.olingo.server.api.uri.queryoption.SelectOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * select 的处理
 * @author chenYX
 * @date 2021/6/28 11:12
 * @description
 */
@Component
public class SelectProcessImpl extends SqlProcess {

    @Autowired
    private SqlProcessChain sqlProcessChain;

    @Override
    public void process(OdataSqlInformation.SqlInformationBuild sqlBuild, UriInfo uriInfo, Class<?> aClass, OData oData) {
        SelectOption selectOption = uriInfo.getSelectOption();
        if (selectOption != null) {
            List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
            UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
            EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
            try {
                String selectList = oData.createUriHelper()
                        .buildContextURLSelectList(edmEntitySet.getEntityType(), null, selectOption);

                if (StringUtils.isNotBlank(selectList)) {
                    OdataClassMetaData odataClassMetaData = OdataCachedData.getClassMetaDataByClass(aClass);
                    Map<String, String> objectFieldToDataBaseFieldMap = odataClassMetaData.getObjectFieldToDataBaseFieldMap();
                    String[] selectedFields = selectList.split(",");
                    List<String> selectFields = new ArrayList<>();
                    for (String selectedField : selectedFields) {
                        String dataBaseField = objectFieldToDataBaseFieldMap.get(selectedField);
                        if (StringUtils.isNotBlank(dataBaseField)) {
                            selectFields.add(dataBaseField);
                        }
                    }
                    sqlBuild.buildSelectAllFieldsFlag(false).buildSelectFields(selectFields);
                }
            } catch (SerializerException e) {
                e.printStackTrace();
            }
        } else {
            sqlBuild.buildSelectAllFieldsFlag(true);
        }

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.sqlProcessChain.addSqlProcess(this);
    }


}
